package v1

import (
	"adminHome/models"
	"adminHome/pkg/app"
	"adminHome/pkg/e"
	"adminHome/pkg/util"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"net/http"
)

func GetProduct(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.Query("id")).MustInt()
	var data interface{}

	valid := validation.Validation{}
	valid.Required(id, "id").Message("id不能为空")
	code := e.INVALID_PARAMS
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, data)
		return
	}

	if !models.ExistProductById(id) {
		code = e.ERROR_NOT_EXIST_PRODUCT
		appG.Response(http.StatusOK, code, data)
		return
	}

	code = e.SUCCESS
	data, _ = models.GetProductById(id)
	appG.Response(http.StatusOK, code, data)
}

func GetProductList(c *gin.Context) {
	appG := app.Gin{c}
	name := c.Query("name")

	maps := make(map[string]interface{})
	data := make(map[string]interface{})

	if name != "" {
		maps["name"] = name
	}
	code := e.SUCCESS
	data["list"] = models.GetProductList(util.GetPage(c), util.GetPageSize(c), maps)
	data["total"] = models.GetProductTotal(maps)
	appG.Response(http.StatusOK, code, data)
}

func AddProduct(c *gin.Context) {
	appG := app.Gin{c}
	name := c.PostForm("name")
	type_ := com.StrTo(c.PostForm("type_")).MustInt()
	price := com.StrTo(c.PostForm("price")).MustFloat64()
	spec := c.PostForm("spec")
	imgUrl := c.DefaultPostForm("img_url", "")
	remark := c.DefaultPostForm("remark", "")

	valid := validation.Validation{}

	valid.Required(name, "name").Message("产品名不能为空")
	valid.Required(type_, "type_").Message("产品分类不能为空")
	valid.Required(price, "price").Message("产品价格不能为空")
	valid.Required(spec, "spec").Message("产品规格不能为空")

	code := e.INVALID_PARAMS
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}

	if models.ExistProductByName(name) {
		code = e.ERROR_EXIST_PRODUCT
		appG.Response(http.StatusOK, code, nil)
		return
	}

	code = e.SUCCESS
	data := make(map[string]interface{})
	data["name"] = name
	data["type"] = type_
	data["price"] = price
	data["spec"] = spec
	data["imgUrl"] = imgUrl
	data["remark"] = remark
	data["status"] = 1
	models.AddProduct(data)

	appG.Response(http.StatusOK, code, nil)
}

func EditProduct(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.PostForm("id")).MustInt()
	name := c.PostForm("name")
	type_ := com.StrTo(c.PostForm("type_")).MustInt()
	price := com.StrTo(c.PostForm("price")).MustFloat64()
	spec := c.PostForm("spec")
	imgUrl := c.PostForm("img_url")
	remark := c.PostForm("remark")

	valid := validation.Validation{}
	valid.Required(id, "id").Message("id不能为空")
	valid.Required(name, "name").Message("产品名不能为空")
	valid.Required(type_, "type_").Message("产品分类不能为空")
	valid.Required(price, "price").Message("产品价格不能为空")
	valid.Required(spec, "spec").Message("产品规格不能为空")

	code := e.INVALID_PARAMS
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}

	if models.ExistProductByName(name) {
		code = e.ERROR_EXIST_PRODUCT
		appG.Response(http.StatusOK, code, nil)
		return
	}

	code = e.SUCCESS
	data := make(map[string]interface{})
	data["name"] = name
	data["type"] = type_
	data["price"] = price
	data["spec"] = spec
	data["imgUrl"] = imgUrl
	data["remark"] = remark
	data["status"] = 1
	models.EditProduct(id, data)
	appG.Response(http.StatusOK, code, nil)
}

func DelProduct(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.PostForm("id")).MustInt()

	valid := validation.Validation{}
	valid.Required(id, "id").Message("id不能为空")
	code := e.INVALID_PARAMS
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}

	if !models.ExistProductById(id) {
		code = e.ERROR_NOT_EXIST_PRODUCT
		appG.Response(http.StatusOK, code, nil)
		return
	}
	code = e.SUCCESS
	models.DelProduct(id)
	appG.Response(http.StatusOK, code, nil)
}
